package com.etc.sfts.biz.ipml;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.etc.sfts.biz.CinemaBiz;
import com.etc.sfts.dao.CinemaDao;
import com.etc.sfts.dao.MovieDao;
import com.etc.sfts.dto.PublishCinemaDto;
import com.etc.sfts.dto.PublishMovieDto;
import com.etc.sfts.entity.Cinema;
import com.etc.sfts.entity.Movie;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CinemaBizImpl implements CinemaBiz{
	@Autowired
	private CinemaDao dao ;

	public CinemaDao getDao() {
		return dao;
	}

	public void setDao(CinemaDao dao) {
		this.dao = dao;
	}

	@Override
	public List<Cinema> getCinemaByAddress(String address) {
		if(address==null) {
			return null;
		}
		
		
		List<PublishCinemaDto> cinemaNameList = dao.queryPublishCinemaByAddress(address);
		if(cinemaNameList==null&&cinemaNameList.size()<=0) {
			return null;
		}
		
		List<Cinema> cinemaList= new ArrayList<Cinema>();
		for (int i = 0; i < cinemaNameList.size(); i++) {
			
			String cinemaName = cinemaNameList.get(i).getPublish_cinema();
			List<Cinema> cinema = dao.queryCinemaByCinemaName(cinemaName);
			if(cinema!=null&&cinema.size()>0) {
				cinemaList.add(cinema.get(0));
			}
		}
		return cinemaList;
	}

	@Override
	public boolean insertCinema(Cinema cinema) {
		if(cinema!=null) {
			return dao.addCinema(cinema);
		}
		return false;
	}


	@Override
	public Long getCinemaCount() {
		// TODO Auto-generated method stub
		return dao.queryCinemaCount();
	}

	@Override
	public Cinema getCinemaByName(String name) {
		List<Cinema> list=dao.queryCinemaByCinemaName(name);
		if(list.size()>0&&list!=null) {
			return list.get(0);
	}
		return null;
	}





	@Override
	public List<Cinema> queryCinemaByManagerId(Integer managerId) {
	
		return dao.queryCinemaByManagerId(managerId);
	}

	@Override
	public List<Cinema> getAllCinema(Integer page, Integer size) {
		// TODO Auto-generated method stub
		return dao.queryAllCinema(page, size);
	}

	@Override
	public boolean deleteCinemaById(Integer id) {
		// TODO Auto-generated method stub
		return dao.deleteCinema(id);
	}

	@Override
	public HSSFWorkbook exportExcel(Map<String, Object> map) {
		//获取需要生成excel的数据
		List<Cinema> list = dao.getCinemaToExcel(map);
		//判断是否为空
		if (list == null) {
			return null;
		}

		HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
		//创建一个excel的sheet
		HSSFSheet sheet = hssfWorkbook.createSheet("firstshett");
		//表头属性名
		String[] propertyName = {"ID","影院名称","影院地区","影院具体地址","影院电话","影院距离","影院票价"};
		//获取表所需要的数量
		int length = propertyName.length;
		//-------设置第一行---------
		//设置行下面为第一行
		HSSFRow row = sheet.createRow(0);
		//设置列下面为第一行的第一列
		HSSFCell hssfCell = row.createCell(0);


		//设置字体
		HSSFFont hssfFont = hssfWorkbook.createFont();
		hssfFont.setFontName("黑体");
		//字体大小
		hssfFont.setFontHeightInPoints((short) 12);
		//设置样式
		HSSFCellStyle hssfCellStyle = hssfWorkbook.createCellStyle();
		//使用上面的字体样式
		hssfCellStyle.setFont(hssfFont);
		hssfCellStyle.setLocked(true);
		//自动换行
		hssfCellStyle.setWrapText(false);
		CellRangeAddress rangeAddress = new CellRangeAddress(0, 0, 0, length - 1);
		//把合并单元格设置进去
		sheet.addMergedRegion(rangeAddress);
		//设置样式
		hssfCell.setCellStyle(hssfCellStyle);
		hssfCell.setCellValue("车辆列表信息");
		//----------设置第二行-----------
		HSSFRow row2 = sheet.createRow(1);
		//字段名的字体
		HSSFFont hssfFont1 = hssfWorkbook.createFont();
		hssfFont1.setFontName("宋体");
		//字体大小
		hssfFont1.setFontHeightInPoints((short) 12);
		//列表样式
		HSSFCellStyle hssfCellStyle1 = hssfWorkbook.createCellStyle();
		//设置字体字体样式
		hssfCellStyle1.setFont(hssfFont1);
		hssfCellStyle1.setLocked(true);
		hssfCellStyle1.setWrapText(false);

		//设置行高
		row2.setHeight((short) 750);
		//创建列
		HSSFCell cell = null;
		for (int i = 0; i < length; i++) {
			cell = row2.createCell(i);
			//设置字段名
			cell.setCellValue(propertyName[i]);
			//设置样式
			cell.setCellStyle(hssfCellStyle1);
			//----设置表里面的内容--------------
			//设置字体
			HSSFFont font=hssfWorkbook.createFont();
			font.setFontName("宋体");
			font.setFontHeightInPoints((short)10);
			//设置样式
			HSSFCellStyle hssfCellStyle2=hssfWorkbook.createCellStyle();
			hssfCellStyle2.setFont(font);
			hssfCellStyle2.setWrapText(true);
			HSSFRow hssfRow=null;
			HSSFCell hssfCell1=null;
			int valueStatrRow=2;
			//赋值
			for (Cinema cinema:list
			) {
				hssfRow=sheet.createRow(valueStatrRow);
				//id
				hssfCell1=hssfRow.createCell(0);
				hssfCell.setCellStyle(hssfCellStyle2);
				hssfCell1.setCellValue(cinema.getCinema_id());
				//影院名称
				hssfCell1=hssfRow.createCell(1);
				hssfCell.setCellStyle(hssfCellStyle2);
				hssfCell1.setCellValue(cinema.getCinema_name());
				//影院地区
				hssfCell1=hssfRow.createCell(2);
				hssfCell.setCellStyle(hssfCellStyle2);
				hssfCell1.setCellValue(cinema.getCinema_area());
				//影院具体地址
				hssfCell1=hssfRow.createCell(3);
				hssfCell.setCellStyle(hssfCellStyle2);
				hssfCell1.setCellValue(cinema.getCinema_address());
				//影院电话
				hssfCell1=hssfRow.createCell(4);
				hssfCell.setCellStyle(hssfCellStyle2);
				hssfCell1.setCellValue(cinema.getCinema_phone());
				//影院距离
				hssfCell1=hssfRow.createCell(5);
				hssfCell.setCellStyle(hssfCellStyle2);
				hssfCell1.setCellValue(cinema.getCinema_distance());
				//影院票价
				hssfCell1=hssfRow.createCell(6);
				hssfCell.setCellStyle(hssfCellStyle2);
				hssfCell1.setCellValue(cinema.getCinema_price());


				valueStatrRow++;



			}

		}
		return hssfWorkbook;
	}
}
